package com.designpattern.demo.ui.act;

import android.content.Intent;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.designpattern.demo.R;
import com.designpattern.demo.R2;
import com.designpattern.demo.design1.SimpleFactoryActivity;
import com.designpattern.demo.design2.StrategyActivity;
import com.designpattern.demo.design3.DecoratorActivity;
import com.designpattern.demo.ui.BaseActivity;
import com.designpattern.demo.ui.adapter.DesignPatternListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author lingc
 */
public class MainActivity extends BaseActivity {

    @BindView(R2.id.rv_design_pattern)
    RecyclerView mRvDesignPattern;

    @Override
    protected int getContentResId() {
        return R.layout.activity_main;
    }

    private DesignPatternListAdapter mDesignPatternListAdapter;

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        mRvDesignPattern.setLayoutManager(layoutManager);
        mDesignPatternListAdapter = new DesignPatternListAdapter(mStringList);
        mRvDesignPattern.setAdapter(mDesignPatternListAdapter);
        mDesignPatternListAdapter.setOnItemClickListener((baseQuickAdapter, view, position) -> {
            Intent intent;
            switch (position) {
                case 0:
                    intent = new Intent(MainActivity.this, SimpleFactoryActivity.class);
                    break;
                case 1:
                    intent = new Intent(MainActivity.this, StrategyActivity.class);
                    break;
                case 2:
                    intent = new Intent(MainActivity.this, DecoratorActivity.class);
                    break;
                default:
                    intent = new Intent(MainActivity.this, StrategyActivity.class);
                    break;
            }
            startActivity(intent);
        });
    }

    private List<String> mStringList;

    @Override
    protected void initData() {
        if (mStringList == null) {
            mStringList = new ArrayList<>();
        }
        mStringList.clear();
        String[] stringArray = getResources().getStringArray(R.array.designpattern_list);
        for (int i = 0; i < stringArray.length; i++) {
            mStringList.add(stringArray[i]);
        }
        mDesignPatternListAdapter.setNewData(mStringList);
    }


}
